package server.startup;

import server.*;
import server.core.ApplicationFilterConfig;
import server.core.StandardContext;
import server.core.StandardWrapper;
import server.deploy.FilterDef;
import server.deploy.FilterMap;

import javax.servlet.FilterConfig;
import java.util.HashMap;

public class SimpleContextConfig implements LifecycleListener {
    @Override
    public void lifecycleEvent(LifecycleEvent event) {
        if (Lifecycle.START_EVENT.equals(event.getType())) {
            Context context = (Context) event.getLifecycle();
            configServlet(context);
            //配置过滤器
            configFilter(context);
            //配置监听器
            configListener(context);
            //设置支持热部署
            context.setReloadable(true);
            //设置配置成功
            context.setConfigured(true);
        }
    }
    private void configServlet(Context context) {
        Wrapper wrapper1 = new StandardWrapper();
        wrapper1.setName("WrapperTest");
        wrapper1.setServletClass("WrapperTestServlet");
        wrapper1.addInitParameter("w1", "v1");
        wrapper1.addInitParameter("w2", "v2");

        Wrapper wrapper2 = new StandardWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        Wrapper wrapper3 = new StandardWrapper();
        wrapper3.setName("Primitive");
        wrapper3.setServletClass("PrimitiveServlet");

        Wrapper wrapper4 = new StandardWrapper();
        wrapper4.setName("FileReloadServlet");
        wrapper4.setServletClass("reload.servlet.FileReloadServlet");

        Wrapper wrapper5 = new StandardWrapper();
        wrapper5.setName("JarReloadServlet");
        wrapper5.setServletClass("jar.reload.JarReloadServlet");

        context.addChild(wrapper1);
        context.addChild(wrapper2);
        context.addChild(wrapper3);
        context.addChild(wrapper4);
        context.addChild(wrapper5);

        //目录匹配
        context.addServletMapping("/WrapperTest", "WrapperTest");
        //后缀匹配
        context.addServletMapping("/Modern", "Modern");
        //默认匹配
        context.addServletMapping("/Primitive", "Primitive");

        context.addServletMapping("/FileReload", "FileReloadServlet");

        context.addServletMapping("/JarReload", "JarReloadServlet");
    }

    private void configFilter(Context context) {
        FilterDef filterDef1 = newFilterDef("F1", "Filter1");
        FilterDef filterDef2 = newFilterDef("F2", "Filter2");
        context.addFilterDef(filterDef1);
        context.addFilterDef(filterDef2);
        FilterMap filterMap1 = newFilterMap("F1", "Modern", "/wr/*");
        FilterMap filterMap2 = newFilterMap("F2", null, "/wr/f2");
        context.addFilterMap(filterMap1);
        context.addFilterMap(filterMap2);
    }

    private FilterDef newFilterDef(String filterName, String filterClass) {
        FilterDef filterDef = new FilterDef();
        filterDef.setFilterName(filterName);
        filterDef.setFilterClass(filterClass);
        filterDef.addInitParameter(filterName + ".p1", filterName + ".v1");
        filterDef.addInitParameter(filterName + ".p2", filterName + ".v2");
        return filterDef;
    }

    private FilterMap newFilterMap(String filterName, String servletName, String urlPattern) {
        FilterMap filterMap = new FilterMap();
        filterMap.setFilterName(filterName);
        filterMap.setServletName(servletName);
        filterMap.setUrlPattern(urlPattern);
        return filterMap;
    }

    private void configListener(Context context) {
        context.addApplicationListener("listener.ContextListener");
    }

}
